package cn.ht.jywrwater.module.ht.service.waterqualityfactor;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.ht.jywrwater.module.ht.controller.admin.waterqualityfactor.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterqualityfactor.WaterQualityFactorDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.waterqualityfactor.WaterQualityFactorConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.waterqualityfactor.WaterQualityFactorMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 水质因子 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class WaterQualityFactorServiceImpl implements WaterQualityFactorService {

    @Resource
    private WaterQualityFactorMapper waterQualityFactorMapper;

    @Override
    public Long createWaterQualityFactor(WaterQualityFactorCreateReqVO createReqVO) {
        // 插入
        WaterQualityFactorDO waterQualityFactor = WaterQualityFactorConvert.INSTANCE.convert(createReqVO);
        waterQualityFactorMapper.insert(waterQualityFactor);
        // 返回
        return waterQualityFactor.getId();
    }

    @Override
    public void updateWaterQualityFactor(WaterQualityFactorUpdateReqVO updateReqVO) {
        // 校验存在
        validateWaterQualityFactorExists(updateReqVO.getId());
        // 更新
        WaterQualityFactorDO updateObj = WaterQualityFactorConvert.INSTANCE.convert(updateReqVO);
        waterQualityFactorMapper.updateById(updateObj);
    }

    @Override
    public void deleteWaterQualityFactor(Long id) {
        // 校验存在
        validateWaterQualityFactorExists(id);
        // 删除
        waterQualityFactorMapper.deleteById(id);
    }

    private void validateWaterQualityFactorExists(Long id) {
        if (waterQualityFactorMapper.selectById(id) == null) {
            throw exception(WATER_QUALITY_FACTOR_NOT_EXISTS);
        }
    }

    @Override
    public WaterQualityFactorDO getWaterQualityFactor(Long id) {
        return waterQualityFactorMapper.selectById(id);
    }

    @Override
    public List<WaterQualityFactorDO> getWaterQualityFactorList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return waterQualityFactorMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WaterQualityFactorDO> getWaterQualityFactorPage(WaterQualityFactorPageReqVO pageReqVO) {
        return waterQualityFactorMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WaterQualityFactorDO> getWaterQualityFactorList(WaterQualityFactorExportReqVO exportReqVO) {
        return waterQualityFactorMapper.selectList(exportReqVO);
    }

}
